#include "librtsp.h"


RtspClient *gRtspclient;

RtspClient *ConectRtsp(char *ip,int port,char *desc){
    if(0 == ip || 0 == desc){
        return 0;
    }
    RtspClient *ret = (RtspClient*)malloc(sizeof(RtspClient));
    ret->sock = Connect(ip,port);
    if(ret->sock < 0){
        return 0;
    }
    strcpy(ret->hostIp,ip);
    ret->port = port;
    ret->cesq = 1;
    strcpy(ret->despcrtion,desc);
    return ret;
}

int SendOption(RtspClient*p,CallBack *c){
    char pRecv[500];
    char buf[500];
    sprintf(buf,"OPTIONS rtsp://%s:%d/%s RTSP/1.0\r\nCSeq: %d\r\nUser-Agent: Lavf57.71.100\r\n\r\n",p->hostIp,p->port,
            p->despcrtion,p->cesq);
    int ret = Send(p->sock,buf,strlen(buf));
    printf("send %d \r\ndata is:\r\n%s\r\n",ret,buf);
    if(0 > ret){
        CloseConn(p->sock);
        p->conn = FALSE;
        return -1;
    }
    if(Recv(p->sock,pRecv,500) > 0) {
        printf("recv: %s\r\n",pRecv);
    } else{
        printf("error recv ");
        return -1;
    }
}
/*
ANNOUNCE rtsp://118.24.238.198:554/Sword RTSP/1.0
CSeq: 1
User-Agent: EasyPusher v1.2.16.1105
Content-Type: application/sdp
Content-Length: 384

v=0
o=- 0 0 IN IP4 127.0.0.1
s=EasyDarwin
i=EasyDarwin
c=IN IP4 127.0.0.1
t=0 0
a=x-qt-text-nam:EasyDarwin
a=x-qt-text-inf:EasyDarwin
a=x-qt-text-cmt:source application::EasyDarwin
a=x-qt-text-aut:
a=x-qt-text-cpy:
m=video 0 RTP/AVP 96
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;sprop-parameter-sets=,2QAKKy0A8ARPywgAAADACAAAAMD0eMGVA==
a=control:streamid=0

*/
int SendAnnounce(RtspClient *p,CallBack callback){
    char content[] ="v=0\r\n"
    "o=- 0 0 IN IP4 127.0.0.1\r\n"
    "s=EasyDarwin\r\n"
    "i=EasyDarwin\r\n"
    "c=IN IP4 127.0.0.1\r\n"
    "t=0 0\r\n"
    "a=x-qt-text-nam:EasyDarwin\r\n"
    "a=x-qt-text-inf:EasyDarwin\r\n"
    "a=x-qt-text-cmt:source application::EasyDarwin\r\n"
    "a=x-qt-text-aut:\r\n"
    "a=x-qt-text-cpy:\r\n"
    "m=video 0 RTP/AVP 96\r\n"
    "a=rtpmap:96 H264/90000\r\n"
    "a=fmtp:96 packetization-mode=1;sprop-parameter-sets=,2QAKKy0A8ARPywgAAADACAAAAMD0eMGVA==\r\n"
    "a=control:streamid=0\r\n";
    char pRecv[500] = {0};
    char length[10] = {0};
    char buf[500] = {0};
    sprintf(buf,"ANNOUNCE rtsp://%s:%d/%s RTSP/1.0\r\nCSeq: %d\r\nUser-Agent: EasyPusher v1.2.16.1105\r\n"
                "Content-Type: application/sdp\r\n"
                "Content-Length: ",p->hostIp,p->port,p->despcrtion,p->cesq++);
    sprintf(length,"%d",strlen(content));
    strcat(buf,length);
    strcat(buf,"\r\n\r\n");
    strcat(buf,content);

    int ret = Send(p->sock,buf,strlen(buf));
    printf("send %d \r\ndata is:\r\n%s\r\n",ret,buf);
    if(0 > ret){
        CloseConn(p->sock);
        p->conn = FALSE;
        return -1;
    }
    if(Recv(p->sock,pRecv,500) > 0) {
        printf("recv: %s\r\n",pRecv);
    } else{
        printf("error recv ");
        return -1;
    }
}

int SendSetup(RtspClient *p,CallBack *c){
    char pRecv[500] = {0};
    char buf[500] = {0};
    sprintf(buf,"SETUP rtsp://%s:%d/%s/streamid=0 RTSP/1.0\r\nTransport: RTP/AVP/TCP;unicast;mode=record;interleaved=0-1\r\n"
                "CSeq: %d\r\nUser-Agent: EasyPusher v1.2.16.1105\r\n\r\n",p->hostIp,p->port,p->despcrtion,p->cesq++);
    int ret = Send(p->sock,buf,strlen(buf));
    printf("send %d \r\ndata is:\r\n%s\r\n",ret,buf);
    if(0 > ret){
        CloseConn(p->sock);
        p->conn = FALSE;
        return -1;
    }
    int recv = Recv(p->sock,pRecv,500);
    if(ret > 0) {
        printf("recv: %s\r\n",pRecv);
        if(NULL != c){
            c(p,pRecv,recv);
        }
    } else{
        printf("error recv ");
        return -1;
    }
    return 0;
}
/*
RECORD rtsp://118.24.238.198:554/Sword RTSP/1.0
Range: npt=0.000-
CSeq: 4
User-Agent: EasyPusher v1.2.16.1105
Session: BktDfYuIH
 */
int SendRecord(RtspClient *p,CallBack *c){
    char pRecv[500] = {0};
    char buf[500] = {0};
    sprintf(buf,"RECORD rtsp://%s:%d/%s RTSP/1.0\r\nRange: npt=0.000-\r\n"
                "CSeq: %d\r\nUser-Agent: Lavf57.71.100\r\nSession:%s\r\n\r\n",p->hostIp,p->port,p->despcrtion,p->cesq++,p->session);
    int ret = Send(p->sock,buf,strlen(buf));
    printf("send %d \r\ndata is:\r\n%s\r\n",ret,buf);
    if(0 > ret){
        CloseConn(p->sock);
        p->conn = FALSE;
        return -1;
    }  
    if(Recv(p->sock,pRecv,500) > 0) {
        printf("recv: %s\r\n",pRecv);
    } else{
        printf("error recv ");
        return -1;
    }
}

int SendRtpPackage(RtspClient *p,unsigned char *buf,int length) {
    int ret = Send(p->sock,(char*)buf,length);
    //printf("request send %d ,send rtp pack %d \r\n",length,ret,buf);
    if(0 > ret){
        CloseConn(p->sock);
        p->conn = FALSE;
        return -1;
    }
}

int CallBackSetup(RtspClient *client, const char *data, int len) {
    char *session = strstr(data,"Session: ");
    if(NULL != session){
        sscanf(session,"Session: %s\r\n",client->session);
    }
}
